package com.a214.yxz.yuejian013.Fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.a214.yxz.yuejian013.JavaBean.OrderBean;
import com.a214.yxz.yuejian013.Adapter.OrderListAdapter;
import com.a214.yxz.yuejian013.Base.BaseSFragment;
import com.a214.yxz.yuejian013.Listener.ServiceListener;
import com.a214.yxz.yuejian013.Model.ResModel;
import com.a214.yxz.yuejian013.R;

import java.util.List;

/**
 * A simple {@link BaseSFragment} subclass.
 */
public class UserOrderSub2Fragment extends BaseSFragment {

    private RecyclerView recyclerView;
    private ResModel s_model = new ResModel("", getSessionID());
    private ServiceListener<String> s_listener;
    private ResModel model = new ResModel("gson", getSessionID());
    private ServiceListener<List<OrderBean>> listener;
    private OrderListAdapter adapter;
    private RecyclerView.LayoutManager layoutManager;


    public UserOrderSub2Fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_user_order_sub2, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView= (RecyclerView) view.findViewById(R.id.user_order_unFinish_list);
        adapter = new OrderListAdapter(getContext());
        layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(layoutManager);

        listener=new ServiceListener<List<OrderBean>>() {
            @Override
            public void onResponse(final List<OrderBean> l) {
                adapter.setList(l);
                adapter.setOnItemClickListener(new OrderListAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        if (l.get(position).getVstate().equals("0")) {
                            s_model.refund(l.get(position).getId(), l.get(position).getType(), s_listener);
                        } else {
                            Toast.makeText(getContext(), "该订单无法申请退款", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onItemLongClick(View view, int position) {

                    }
                });
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
            }
        };

        s_listener = new ServiceListener<String>() {
            @Override
            public void onResponse(String l) {
                if (l.equals("1")) {
                    Toast.makeText(getContext(), "申请成功", Toast.LENGTH_SHORT).show();
                    getOrderList();
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
            }
        };

        getOrderList();

    }
    private void getOrderList(){
        //model.getOrderByState(getUserId(),1,"0",listener);
        model.getOrderByState("13",1,"0",listener);
    }
}
